package com.jyxd.web.controller.basic;


import com.jyxd.web.data.basic.BedReservation;
import com.jyxd.web.data.common.Result;
import com.jyxd.web.data.user.User;
import com.jyxd.web.service.assessment.basic.BedReservationService;
import com.jyxd.web.util.HttpCode;
import com.jyxd.web.util.JsonArrayValueProcessor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jyxd.web.util.UUIDUtil.getUUID;

@Api(tags = "床位预约")
@RestController
@RequestMapping(value = "/bedReservation")
public class BedReservationController {

    @Autowired
    private BedReservationService bedReservationService;


    @ApiOperation("查询全部")
    @RequestMapping("/findAll")
    public Result findAll() {
        List<BedReservation> list = bedReservationService.list();
        return Result.succeed(list);
    }

    @ApiOperation("删除")
    @RequestMapping("remove/{id}")
    public Result remove(@PathVariable("id") String id) {
        boolean isSuccess = bedReservationService.removeById(id);
        if (isSuccess) {
            return Result.succeed("删除成功");
        } else {
            return Result.failed("删除失败");
        }
    }

    @ApiOperation("条件查询分页")
    @PostMapping("findQueryPage")
    public String findPage(@RequestBody(required = false) Map<String,Object> map,HttpSession session) {
        User user=(User)session.getAttribute("user");
        if (map == null) map = new HashMap<>();
        if(user!=null && StringUtils.isNotEmpty(user.getTenantId())){
            String tenantId = user.getTenantId();
            if(org.apache.commons.lang.StringUtils.isNotEmpty(tenantId)){
                map.put("tenantId",tenantId);
            }
        }

        JSONObject json = new JSONObject();
        if (map != null && map.containsKey("start")) {
            int totalCount = bedReservationService.queryNum(map);
            map.put("start", ((int) map.get("start") - 1) * (int) map.get("size"));
            json.put("totalCount", totalCount);
        }
        List<BedReservation> list = bedReservationService.queryList(map);
        if (list != null && list.size() > 0) {
            JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.registerJsonValueProcessor(Date.class, new JsonArrayValueProcessor());
            json.put("msg", "查询成功");
            json.put("data", JSONArray.fromObject(list,jsonConfig));
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    @ApiOperation("预约床位")
    @PostMapping("saveReservation")
    public Result saveReservation(@RequestBody BedReservation bedReservation, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (StringUtils.isEmpty(bedReservation.getId())) {
            bedReservation.setId(getUUID());
        }
        bedReservation.setTenantId(user.getTenantId());
        boolean isSuccess = bedReservationService.saveOrUpdate(bedReservation);
        if (isSuccess) {
            return Result.succeed("添加成功");
        } else {
            return Result.failed("添加失败");
        }
    }

    //修改-根据id查询
    @ApiOperation("根据id查询")
    @RequestMapping("getReservation/{id}")
    public Result getReservation(@PathVariable("id") String id) {
        BedReservation bedReservation = bedReservationService.getById(id);
        return Result.succeed(bedReservation);
    }

    //修改
    @ApiOperation("确认床位")
    @PostMapping("updateReservation")
    public Result updateReservation(@RequestBody BedReservation bedReservation) {
        BedReservation fromDb = bedReservationService.getById((bedReservation.getId()));
        bedReservation.setTenantId(fromDb.getTenantId());
        boolean isSuccess = bedReservationService.updateById(bedReservation);
        if (isSuccess) {
            return Result.succeed("确认成功");
        } else {
            return Result.failed("确认失败");
        }
    }
}
